package com.panshi.modules.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panshi.modules.domain.SysUser;
import com.panshi.modules.service.SysUserService;
import com.panshi.vo.R;
import com.panshi.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器 :系统用户
 * </p>
 *
 * @author 857
 * @since 2020-07-29
 */
@Api(tags = "系统用户Controller")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("查询系统用户列表")
    @GetMapping("/list")
    public List<SysUserVO> listSysUser(){
        List<SysUserVO> list = sysUserService.listSysUser();
        return list;
    }

    @ApiOperation("查询系统用户分页")
    @GetMapping("/pageUserList/{page}/{limit}")
    public R pageUserList( @PathVariable("page") Integer page,
                           @PathVariable("limit") Integer limit){
        Page<SysUser> pages = new Page<>(page,limit);
        IPage<SysUser> sysUserIPage = sysUserService.pageUserList(pages);
        return R.ok().data("rows",sysUserIPage.getRecords()).data("total",sysUserIPage.getTotal());
    }


    @ApiOperation("新增系统用户接口")
    @PostMapping
    public R saveSysUser(@Validated @RequestBody @ApiParam(value = "SysUserVO" ,name = "新增用户参数") SysUserVO sysUser){

        if(StringUtils.isNotBlank(sysUser.getUsername())){
            if(sysUserService.selectCountByUsername(sysUser.getUsername(),0) > 0){
                return R.error().msg("账号已存在");
            }
        }else {
            return R.error().msg("请输入账号名");
        }

        sysUser.setPassword("123456");
        log.info("新增系统用户={}",sysUser);
        int result = sysUserService.saveSysUser(sysUser);
        if(result > 0){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("编辑系统用户")
    @PutMapping
    public R updateSysUser(@Validated @RequestBody @ApiParam(value = "SysUserVO" ,name = "编辑用户参数") SysUserVO sysUserVO){

        if(StringUtils.isNotBlank(sysUserVO.getUsername())){
            if(sysUserService.selectCountByUsername(sysUserVO.getUsername(),sysUserVO.getId()) > 0){
                return R.error().msg("账号已存在");
            }
        }

        log.info("修改系统用户={}",sysUserVO);
        int result = sysUserService.updateSysUser(sysUserVO);
        if(result > 0){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("删除系统用户接口")
    @ApiImplicitParam(name = "id" , value = "用户id" ,required = true)
    @DeleteMapping("/{id}")
    public R deleteSysUser(@PathVariable("id")Integer id){
        log.info("删除系统用户,ID={}",id);
        int result = sysUserService.deleteSysUser(id);
        if(result > 0){
            return R.ok();
        }
        return R.error();

    }

    @ApiOperation("通过账号名获取系统用户信息")
    @ApiImplicitParam(name = "username" , value = "用户账号" ,required = true)
    @GetMapping("/{username}")
    public SysUserVO getByUsername(@PathVariable("username")String username){
        log.info("getByUsername--------------------------------------------- :{}",username);
        SysUserVO userVO = sysUserService.getSysUserByUsername(username);
        log.info("userVO--------------------------------------------- :{}",userVO);

        return userVO;
    }


}

